#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#


"""Tests for the module metadata_in_memory."""

import logging
import unittest
from google.appengine.ext import db
import constants
import db_utils
from metadata_in_memory import MetadataInMemory
import text_data_reader
from tree_node import PageTree


class MetadataInMemoryTest(unittest.TestCase):
  """Tests for the class MetadataInMemory."""

  def setUp(self):
    """Set up method."""
    logging.info('TEST: ' + self.id())
    self.metadata_in_memory = MetadataInMemory()
    self.db_handler = db_utils.DbHandler()
    self.page_tree = PageTree()

  def testPopulateMetadataInMemory(self):
    """Tests the method PopulateMetadataInDb."""
    # Create new version.
    version_id = self.db_handler.CreateNewVersion()
    count = text_data_reader.ReadMetadataFile(
        open('test/data/metadata_valid.txt', 'r'), version_id)
    self.assertEqual(count, 4)
    self.assert_(not self.metadata_in_memory.IsEmpty())
    self.assertEqual(self.metadata_in_memory.GetNumberOfRemainingRows(), 6)
    rows = self.metadata_in_memory.PopulateMetadataInDb()
    if constants.NO_OF_ROWS_IN_SET > 6:
      compare_with = 6
    else:
      compare_with = constants.NO_OF_ROWS_IN_SET
    self.assertEqual(compare_with, rows)
    q = db.GqlQuery('SELECT * FROM Metadata WHERE version_id=:1',
                    version_id)
    self.assertEqual(compare_with, q.count())
    self.db_handler.RemoveVersion(version_id)
    self.metadata_in_memory.Clear()

  def testUpdateMetadata(self):
    # Create new version.
    version_id = self.db_handler.CreateNewVersion()
    count = text_data_reader.ReadMetadataFile(
        open('test/data/metadata_valid.txt', 'r'), version_id)
    self.assertEqual(count, 4)
    self.metadata_in_memory.UpdateMetadata({}, {}, {})
    f = open('test/data/metadata_valid.txt', 'r')
    f.readline()
    f.readline()
    f.readline()
    index = 2
    for line in f:
      columns = text_data_reader.ReadLine(line)
      current_row = self.metadata_in_memory._metadata_rows[index]
      self.assertEqual(columns[0], current_row.id)
      self.assertEqual(columns[1], current_row.sorting_column1)
      self.assertEqual(columns[2], current_row.sorting_column2)
      self.assertEqual(columns[3], current_row.sorting_column3)
      self.assertEqual(columns[4], current_row.title)
      index += 1
    self.db_handler.RemoveVersion(version_id)
    self.metadata_in_memory.Clear()
    self.page_tree.Clear()


def main(unused_argv):
  unittest.main()
